﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BabyFood.Data;
using BabyFood.Entities;

public partial class Admin_DanhMuc_DMBoSuuTap : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCatalog.BindNhomAlbum(cboNhomAlbum);
            BindCatalog.AlbumNhom(cboLoaiNhomAlbum);
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }

    protected void Bind_Data(int PageNo)
    {
        try
        {
            int TotalRows = 0;
            string WhereClause = "1=1";
            DataSet dsBoSuuTap = DataRepository.Provider.ExecuteDataSet("BoSuuTap_GetPaged", WhereClause, "Id DESC", PageNo - 1, Pager1.PageSize);
            gridCatalog.DataSource = dsBoSuuTap.Tables[0];
            gridCatalog.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsBoSuuTap.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }
        catch (Exception)
        {
        }
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(currnetPageIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("DMBoSuuTap.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    public string GetLienKets(object LienKet)
    {
        //if (Session["KhachHangVip"] != null)
        //{
        return Server.HtmlDecode(LienKet.ToString());
        //    }
        //    else
        //        return "Bạn phải đăng nhập";
    }

    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            BoSuuTap entBoSuuTap = new BoSuuTap();
            entBoSuuTap.Id = Convert.ToInt32(H_CatalogId.Value);
            entBoSuuTap.NhomAlbum = cboNhomAlbum.SelectedValue;
            entBoSuuTap.IdNhomAlbum = Convert.ToInt32(cboNhomAlbum.SelectedValue);
            entBoSuuTap.Ten = Common.StandardString(txtTen.Text);
            entBoSuuTap.DienGiai = Server.HtmlEncode(txtDienGiai.Text);
            entBoSuuTap.TomTat = Server.HtmlEncode(txtTomTat.Text);
            entBoSuuTap.TrangThai = chkSuDung.Checked;
            entBoSuuTap.NgayCapNhat = Common.GetServerDateTime();

            if (entBoSuuTap.Id == 0)
            {
                entBoSuuTap.Id = 0;
                updateStatus = DataRepository.BoSuuTapProvider.Insert(entBoSuuTap);
            }
            else
            {
                entBoSuuTap.EntityState = EntityState.Changed;
                updateStatus = DataRepository.BoSuuTapProvider.Update(entBoSuuTap);
            }
            if (updateStatus)
            {
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);
                cmdReset_Click(sender, e);
            }
            else
                WebMessage.Show("Cập nhật Bộ sưu tập thành công!");
        }
        catch (Exception)
        {
            WebMessage.Show("Cập nhật Bộ sưu tập thất bại!");
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        txtTen.Text = "";
        txtDienGiai.Text = "";
        txtTomTat.Text = "";
        chkSuDung.Checked = true;
        H_CatalogId.Value = "0";
        BindCatalog.AlbumNhom(cboLoaiNhomAlbum);
        BindCatalog.BindNhomAlbum(cboNhomAlbum);
        Bind_Data(Pager1.CurrentIndex);
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
        }
    }

    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            try
            {
                H_CatalogId.Value = e.CommandArgument.ToString();
                BoSuuTap entBoSuuTap = DataRepository.BoSuuTapProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                if (entBoSuuTap != null)
                {
                    txtTen.Text = entBoSuuTap.Ten;
                    cboLoaiNhomAlbum.SelectedValue = entBoSuuTap.NhomAlbum;
                    cboNhomAlbum.SelectedValue = entBoSuuTap.IdNhomAlbum.ToString();
                    txtDienGiai.Text = GetLienKets(entBoSuuTap.DienGiai);
                    chkSuDung.Checked = entBoSuuTap.TrangThai;
                    txtTomTat.Text = Server.HtmlDecode(entBoSuuTap.TomTat);
                }
            }
            catch (Exception)
            {
            }
            Bind_Data(Pager1.CurrentIndex);
        }
        if (e.CommandName == "Delete")
        {
            try
            {
                TList<HinhAnh> Tlist_HinhAnh = DataRepository.HinhAnhProvider.GetByIdBoSuuTap(Convert.ToInt32(e.CommandArgument.ToString()));
                if (Tlist_HinhAnh.Count > 0)
                {
                    string sFilePath = Server.MapPath(Common.sPathUpBoSuuTap);
                    foreach (HinhAnh entHinhAnh in Tlist_HinhAnh)
                    {
                        string sThumbPath = sFilePath + "\\" + entHinhAnh.Id.ToString() + "\\Thumb\\" + entHinhAnh.AnhDaiDien;
                        string sFullPath = sFilePath + "\\" + entHinhAnh.Id.ToString() + "\\Full\\" + entHinhAnh.AnhDaiDien;
                        if (System.IO.File.Exists(sThumbPath))
                        {
                            System.IO.File.Delete(sThumbPath);
                        }
                        if (System.IO.File.Exists(sFullPath))
                        {
                            System.IO.File.Delete(sFullPath);
                        }
                        DataRepository.HinhAnhProvider.Delete(entHinhAnh.Id);
                    }
                }
                bool deleteStatus = DataRepository.BoSuuTapProvider.Delete(Convert.ToInt32(e.CommandArgument.ToString()));
                if (!deleteStatus)
                {
                    WebMessage.Show("Xóa Bộ sưu tập thất bại");
                }
            }
            catch (Exception)
            {
                WebMessage.Show("Xóa Bộ sưu tập thất bại");
            }
        }
    }

    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        txtTen.Text = "";
        txtDienGiai.Text = "";
        BindCatalog.BindNhomAlbum(cboNhomAlbum);
        BindCatalog.AlbumNhom(cboLoaiNhomAlbum);
        chkSuDung.Checked = true;
        H_CatalogId.Value = "0";
        txtTomTat.Text = "";
        Pager1.CurrentIndex = 1;
        Bind_Data(Pager1.CurrentIndex);
    }
    public string GetNhomAlbum(object id)
    {
        int s = Convert.ToInt32(id.ToString());
        NhomAlbum ent_NhomAlbum = DataRepository.NhomAlbumProvider.GetById(s);
        return ent_NhomAlbum.TenNhomAlbum;
    }
}
